<?php

namespace addons\shopro\controller\user;

use addons\shopro\controller\Common;
use addons\shopro\service\user\UserAuth;
use addons\shopro\service\Wallet as WalletService;
use app\common\model\GBillModel;
use app\common\model\ScorecModel;
use app\common\model\UserRepurchase;
use app\common\model\UserSGold;
use tests\thinkphp\library\think\buildTest;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use app\common\library\Auth;


class UserPoint extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        \think\Lang::load(APP_PATH . 'api/lang/zh-cn/user.php');        // 加载语言包
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function userSpreads(){
        $user = auth_user();
        $params = $this->request->only(['re_u_id','phonelike']);
        if(!isset($params['re_u_id']) || empty($params['re_u_id'])){
            $u_id = $user['id'];
        }else{
            $u_id = $params['re_u_id'];
        }
        //获取下级所有人的ID包括自己
        $uIds = [$u_id];
        $newArr = [$u_id];
        //查询这个数字的下级
        while (true){
            $uIdss = (new \app\common\model\User())->whereIn('parent_user_id',$newArr)->column('id');

            if(!$uIdss){
                break;
            }
            $uIds = array_merge($uIds,$uIdss);
            $newArr = $uIdss;
        }
        $users = (new \app\common\model\User());
        if(isset($params['phonelike']) && !empty($params['phonelike'])){
            $users->whereLike('mobile','%'.$params['phonelike'].'%');
        }else{
            $users->where('parent_user_id',$u_id);
        }
        $this->success('',$users->with('pointInfo')
            ->with('spreadInfo')->select());
    }

    public function userPointSet(){
        //设置某个人的点位到某个人下
        $params = $this->request->only(['re_u_id','point_uid']);
        if(!isset($params['re_u_id']) || empty($params['re_u_id'])){
            $this->error('请选择需安置用户');
        }
        if(!isset($params['point_uid']) || empty($params['re_u_id'])){
            $this->error('请选择安置节点');
        }

        if($params['re_u_id'] == $params['point_uid']){
            $this->error('安置节点不能是自身');
        }
        $info = (new \app\common\model\User())->where('id',$params['re_u_id'])->find();
        if($info['point_uid'] != 0){
            $this->error('节点已安置!');
        }

        if((new \app\common\model\User())->where('id',$params['re_u_id'])->update([
            'point_uid'=>$params['point_uid']
        ]) === false){
            $this->error('安置失败');
        }
        $this->success();
    }

    /**
     * @desc 添加下级的账号
     * @return void
     */
    public function addCount(){
        $params = $this->request->only(['mobile','password','parent_user_id','point_uid']);
        $this->svalidate($params, '.addCount');
        $user = auth_user( );
        $userAuth = new UserAuth( );
        $userAuth->register( $params );
        $this->success( );
    }


    public function gbills(){
        $type = $this->request->get('type','0');
        $user = auth_user();
        if($type == 0){
            $list = (new GBillModel())->where('user_id',$user['id'])->where('nums','>',0)->order('createtime','DESC')->select();
        }else{
            $list = (new GBillModel())->where('user_id',$user['id'])->where('nums','<',0)->order('createtime','DESC')->select();
        }

        $this->success('',$list);
    }


    public function cbills(){
        $type = $this->request->get('type','0');
        $user = auth_user();
        if($type == 0){
            $list = (new ScorecModel())->where('user_id',$user['id'])->where('nums','>',0)->order('createtime','DESC')->select();
        }else{
            $list = (new ScorecModel())->where('user_id',$user['id'])->where('nums','<',0)->order('createtime','DESC')->select();
        }

        $this->success('',$list);
    }


    public function recharge()
    {
        $params = $this->request->only(['money','r_id']);
        $user = auth_user();
        if($user['u_level'] != 2){
            $this->error('需要vip才可以转账');
        }
        //判断用户等级
        Db::startTrans();
        try {
            //并且扣除自己的钱
            WalletService::change($user['id'],'money',-($params['money']),'recharge','充值扣款');
            WalletService::change($params['r_id'], 'money', $params['money'], 'recharge', [], '充值余额');
            Db::commit();
            $this->success();
        }catch (Exception $exception){
            Db::rollback();
            $this->error();
        }


    }



    public function repurchasebills(){
        $type = $this->request->get('type','0');
        $user = auth_user();
        if($type == 0){
            $list = (new UserRepurchase())->where('user_id',$user['id'])->where('nums','>',0)->order('createtime','DESC')->select();
        }else{
            $list = (new UserRepurchase())->where('user_id',$user['id'])->where('nums','<',0)->order('createtime','DESC')->select();
        }

        $this->success('',$list);
    }


    public function sgoldbills(){
        $type = $this->request->get('type','0');
        $user = auth_user();
        if($type == 0){
            $list = (new UserSGold())->where('user_id',$user['id'])->where('nums','>',0)->order('createtime','DESC')->select();
        }else{
            $list = (new UserSGold())->where('user_id',$user['id'])->where('nums','<',0)->order('createtime','DESC')->select();
        }

        $this->success('',$list);
    }

    public function myPerAccounts()
    {
        $user = auth_user();
        $this->success('',(new \app\common\model\User())->where('pp_user_id',$user['id'])->select());
    }


    public function setPerAccountsPass()
    {
        $params = $this->request->post();
        $userRe = (new \app\common\model\User())->where('id',$params['re_user_id'])->find();
        if(!$userRe){
            $this->error('账号不存在~');
        }
        $res = (new \app\common\model\User())->where('id',$userRe['id'])->update([
            'password'=>(new Auth())->getEncryptPassword($params['password'], $userRe['salt'])
        ]);
        if($res !== false){
            $this->success();
        }
        $this->error();

    }

}